package com.egg.ylt.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.egg.ylt.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created By Dlx
 * on 2018/12/19
 */
public class RingChartView extends View {
    private int mHeight;
    private int mWidth;
    private float ringsMargin = 50;
    private float outRingMargin = 5;
    private List<Integer> colorList = new ArrayList<>();
    private List<Float> nodeList = new ArrayList<>();

    public RingChartView(Context context) {
        super(context);
    }

    public RingChartView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RingChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RingChartView);
        ringsMargin = array.getDimension(R.styleable.RingChartView_two_ring_margin, 10);
        outRingMargin = array.getDimension(R.styleable.RingChartView_out_ring_margin, 0);
        maxData = array.getFloat(R.styleable.RingChartView_ring_maxData, 100);
        setWillNotDraw(false);
        int color = array.getColor(R.styleable.RingChartView_ring_part_1st_color, -1);
        if (color != -1) {
            colorList.add(color);
        }
        color = array.getColor(R.styleable.RingChartView_ring_part_2nd_color, -1);
        if (color != -1) {
            colorList.add(color);
        }
        color = array.getColor(R.styleable.RingChartView_ring_part_3rd_color, -1);
        if (color != -1) {
            colorList.add(color);
        }
        color = array.getColor(R.styleable.RingChartView_ring_part_4th_color, -1);
        if (color != -1) {
            colorList.add(color);
        }
        color = array.getColor(R.styleable.RingChartView_ring_part_5th_color, -1);
        if (color != -1) {
            colorList.add(color);
        }
        color = array.getColor(R.styleable.RingChartView_ring_part_6th_color, -1);
        if (color != -1) {
            colorList.add(color);
        }
        color = array.getColor(R.styleable.RingChartView_ring_part_7th_color, -1);
        if (color != -1) {
            colorList.add(color);
        }
        color = array.getColor(R.styleable.RingChartView_ring_part_8th_color, -1);
        if (color != -1) {
            colorList.add(color);
        }
        color = array.getColor(R.styleable.RingChartView_ring_part_9th_color, -1);
        if (color != -1) {
            colorList.add(color);
        }

        // value
        float node = array.getFloat(R.styleable.RingChartView_ring_node_1st_value, -1);
        if (node != -1) {
            nodeList.add(node);
        }
        node = array.getFloat(R.styleable.RingChartView_ring_node_2nd_value, -1);
        if (node != -1) {
            nodeList.add(node);
        }
        node = array.getFloat(R.styleable.RingChartView_ring_node_3rd_value, -1);
        if (node != -1) {
            nodeList.add(node);
        }
        node = array.getFloat(R.styleable.RingChartView_ring_node_4th_value, -1);
        if (node != -1) {
            nodeList.add(node);
        }
        node = array.getFloat(R.styleable.RingChartView_ring_node_5th_value, -1);
        if (node != -1) {
            nodeList.add(node);
        }
        node = array.getFloat(R.styleable.RingChartView_ring_node_6th_value, -1);
        if (node != -1) {
            nodeList.add(node);
        }
        node = array.getFloat(R.styleable.RingChartView_ring_node_7th_value, -1);
        if (node != -1) {
            nodeList.add(node);
        }
        node = array.getFloat(R.styleable.RingChartView_ring_node_8th_value, -1);
        if (node != -1) {
            nodeList.add(node);
        }
        node = array.getFloat(R.styleable.RingChartView_ring_node_9th_value, -1);
        if (node != -1) {
            nodeList.add(node);
        }

        for (float f : nodeList
                ) {
            Log.e("ond: node:", f + "");
        }
    }


    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Matrix matrix = new Matrix();
    RectF outRing/* = new RectF(10, 10, 210, 210)*/;
    RectF innerRing /*= new RectF(30, 30, 190, 190)*/;
    int diameter;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mHeight = getHeight();
        mWidth = getWidth();
        diameter = mHeight > mWidth ? mWidth : mHeight;
        getOutRing(diameter);
    }

    private void getOutRing(int diameter) {
        outRing = new RectF(outRingMargin, outRingMargin, diameter - outRingMargin, diameter - outRingMargin);
        innerRing = new RectF(ringsMargin + outRingMargin, ringsMargin + outRingMargin, diameter - ringsMargin - outRingMargin, diameter - ringsMargin - outRingMargin);
    }

    private void drawPointer(Canvas canvas) {

    }

    public void setCurrentData(float currentData) {
        this.currentData = currentData;
        invalidate();
    }

    public void setMaxData(float maxData) {
        this.maxData = maxData;
    }

    private float maxData;
    private float currentData = 0;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e( "onDraw: ","invoked" );
        mPaint.setStrokeCap(Paint.Cap.BUTT); //设置笔触 方形
        mPaint.setStrokeWidth(20);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        float startAngle = 135;
        float sweepAngle = 270 / colorList.size();
        for (int i = 0, count = colorList.size(); i < count; i++) {
            mPaint.setColor(colorList.get(i));
            canvas.drawArc(outRing, startAngle, sweepAngle, false, mPaint);
            startAngle += sweepAngle;
        }

//        canvas.drawArc(outRing, 135, 90, false, mPaint);
//        mPaint.setColor(Color.BLUE);
//        canvas.drawArc(outRing, 225, 90, false, mPaint);
//        mPaint.setColor(Color.GREEN);
//        canvas.drawArc(outRing, 315, 90, false, mPaint);
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(5);
        canvas.drawArc(innerRing, 135, 270, false, mPaint);

//        canvas.save();
        canvas.rotate(225, diameter / 2, diameter / 2);
        int tempPoint = 0;
        float cMin = 0, cMax = 1;
        for (int i = 0, count = colorList.size(); i < count; i++) {
            if (currentData > nodeList.get(i)) {
                tempPoint = i;
                cMin = nodeList.get(i);
                if (i + 1 >= count) {
//                    throw new IndexOutOfBoundsException("Your currentData was bigger than maxData,please check it out");
                }
                cMax = nodeList.get(i + 1);
            } else {
                if (i == 0) {
                    cMin = 0;
                    cMax = nodeList.get(i);
                }
                return;
            }
        }
        float degrees = (tempPoint * sweepAngle) + ((currentData / cMax) * sweepAngle);
        Log.e("onDraw: ", degrees + " sweep:" + sweepAngle + "temppoint:" + tempPoint + "current:" + currentData);

        canvas.rotate((tempPoint * sweepAngle) + (currentData / cMax * sweepAngle), diameter / 2, diameter / 2);
        canvas.drawLine(mWidth / 2, outRingMargin + ringsMargin, mWidth / 2, outRingMargin + ringsMargin - 20, mPaint);
//        canvas.restore();

    }
}
